package com.wanyang.controller;

import cn.hutool.core.util.ArrayUtil;
import com.wanyang.service.SmsService;
import com.wanyang.vo.ApiResult;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("common")
public class CommonController {

    @Autowired
    private SmsService smsService;

    @Data
    public static class SendSmsCodeDTO {
        String mobile;
        String scenes;
    }

    @ApiOperation(value = "发送短信验证码", notes = "scenes 目前可选值：\"register\", \"login\",")
    @PostMapping("smsCode")
    public ApiResult sendSmsCode(@RequestBody SendSmsCodeDTO sendSmsCodeDTO) {
        if (!ArrayUtil.contains(SmsService.allowScenes, sendSmsCodeDTO.scenes)) {
            return ApiResult.error(ApiResult.ERROR_REQUEST, null, "scenes is not supported");
        }

        smsService.send(sendSmsCodeDTO.mobile, sendSmsCodeDTO.scenes);

        return ApiResult.success();
    }


}
